package com.net.job;

import java.net.InetAddress;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.net.model.Website;
import com.net.model.WebsiteFlow;
import com.net.model.WebsiteFlowStatData;
import com.net.service.website.WebsiteService;
import com.net.util.IpUtil;
import com.net.util.SpringUtils;

@Component
public class UpdateWebsiteJob {
	public void updateWebsite(){
		System.out.println("UpdateWebsiteJob executed!");
		WebsiteService websiteService = SpringUtils.getBean("websiteService");
		WebsiteFlowStatData websiteFlowStatData = SpringUtils.getBean("website_flow_data");
		System.out.println("执行后网站数据:" + websiteFlowStatData);
		Map<String, WebsiteFlow> data = websiteFlowStatData.getData();
		
		Double[] d = new Double[30];
		for(int i = 0; i < 30; i ++){
			d[i] = 0.0;
		}
		
		//获取数据库中要监控的域名
		List<Website> websiteList = websiteService.listAll();
		for(Website currentWebsite: websiteList){
			String domain = currentWebsite.getDomain();
			//依次查询IP
			InetAddress[] ips = IpUtil.getIpByDomain(domain);
			
			//将查询出的IP放入map中
			for (InetAddress ip : ips) {
				String addr = ip.getHostAddress();
				WebsiteFlow tempwebsiteFlow = new WebsiteFlow(currentWebsite.getType().getName(),currentWebsite.getDomain(),d,0);
				data.put(addr, tempwebsiteFlow);
			}
		}
		websiteFlowStatData.setData(data);
		 websiteFlowStatData = SpringUtils.getBean("website_flow_data");
		System.out.println("执行后网站数据:" + websiteFlowStatData);
	}
}
